#include "stdafx.h"
#include "VODAS.h"
#include "VODASDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CVODASDlg::CVODASDlg( CWnd * pParent /*=NULL*/ ) :
	CDialog( CVODASDlg::IDD, pParent )
{
	m_hIcon = AfxGetApp()->LoadIcon( IDR_MAINFRAME );
}

CVODASDlg::~CVODASDlg( VOID )
{
}

void CVODASDlg::DoDataExchange( CDataExchange * pDX )
{
	CDialog::DoDataExchange( pDX );
}

BEGIN_MESSAGE_MAP( CVODASDlg, CDialog )
	ON_WM_PAINT(															)
	ON_WM_QUERYDRAGICON(													)
	ON_REGISTERED_MESSAGE(	WM_TRAY_ICON_NOTIFY_MESSAGE,	OnTrayNotify	)
	ON_MESSAGE(				WM_HOTKEY,						OnHotKey		)
	ON_WM_DESTROY()
END_MESSAGE_MAP()

BOOL CVODASDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon( m_hIcon, TRUE	);
	SetIcon( m_hIcon, FALSE	);

	return CEventHookerWraper::GetInstance().InitializeTray( this->m_hWnd, this->m_hIcon, _T( " V O D A S " ) );
}

void CVODASDlg::OnPaint()
{
	if( IsIconic() )
	{
		CPaintDC dc( this );

		SendMessage( WM_ICONERASEBKGND, reinterpret_cast< WPARAM >( dc.GetSafeHdc() ), 0 );

		int cxIcon = GetSystemMetrics( SM_CXICON );
		int cyIcon = GetSystemMetrics( SM_CYICON );
		
		CRect rect;
		GetClientRect( &rect );
		int x = ( rect.Width()	- cxIcon + 1) / 2;
		int y = ( rect.Height()	- cyIcon + 1) / 2;

		dc.DrawIcon( x, y, m_hIcon );
	}
	else
		CDialog::OnPaint();
}

HCURSOR CVODASDlg::OnQueryDragIcon()
{
	return static_cast< HCURSOR >( m_hIcon );
}

LRESULT CVODASDlg::OnHotKey( WPARAM wParam, LPARAM lParam )
{
	try
	{
		HWND hForegroundWindow = ::GetForegroundWindow();
		if( hForegroundWindow )
		{
			TCHAR szClassName[ ARRAY_SIZE ] = { 0, };
			if( ::GetClassName( hForegroundWindow, szClassName, ARRAY_SIZE ) )
			{
				if( !::IsWindowVisible( hForegroundWindow ) )
					return -1;

				if( ::GetDesktopWindow() == hForegroundWindow ||
					!_tcsicmp( szClassName, _T( "Shell_TrayWnd" ) ) )
					return -2;

				::SendMessage( hForegroundWindow, CEventHookerWraper::GetInstance().GetTrayMessageID(), NULL, NULL );
			}
		}
	}
	catch( TCHAR * pszError )
	{
		::OutputDebugString( pszError );
	}
	catch( ... )
	{
	}

	return 1;
}

VOID CVODASDlg::OnDestroy( VOID )
{
	CDialog::OnDestroy();

	CEventHookerWraper::GetInstance().UnInitializeTray();
}

LRESULT CVODASDlg::OnTrayNotify( WPARAM wParam, LPARAM lParam )
{
	try
	{
		if( WM_RBUTTONDOWN == lParam ||
			WM_CONTEXTMENU == lParam )
		{
			CPoint pt;
			if( ::GetCursorPos( &pt ) )
			{
						TCHAR szWindowCaptions[ ARRAY_SIZE ][ 100 ] = { 0, };
						CEventHookerWraper::GetInstance().GetMinimizedWindowsList( szWindowCaptions );

						HMENU hContextMenu	= ::CreatePopupMenu();
						HMENU hSubMenu		= ::CreatePopupMenu();

						INT nMinimizedAppCount = 0;
						for( nMinimizedAppCount = 0; nMinimizedAppCount < 1024; nMinimizedAppCount++ )
							if( wcslen( szWindowCaptions[ nMinimizedAppCount ] ) )
								::AppendMenu( hSubMenu, MF_STRING, MENUID_APPNAMES + nMinimizedAppCount , szWindowCaptions[ nMinimizedAppCount ] );
							else
								break;

						LONG nMenuProperties = MF_STRING;
						if( !nMinimizedAppCount )
							nMenuProperties |= MF_GRAYED;

						::AppendMenu( hContextMenu, nMenuProperties | MF_POPUP,	( UINT_PTR )hSubMenu,	_T( "UnTray" )				);
						::AppendMenu( hContextMenu, nMenuProperties,			MENUID_UNTRAY_ALL,		_T( "UnTray All" )			);
						::AppendMenu( hContextMenu, MF_SEPARATOR,				MENUID_SEPARATOR,		_T( "Tray Me Separator" )	);
						::AppendMenu( hContextMenu, MF_STRING,					MENUID_ABOUT_DLG,		_T( "About Tray Me !" )		);
						::AppendMenu( hContextMenu, MF_SEPARATOR,				MENUID_SEPARATOR,		_T( "Tray Me Separator" )	);
						::AppendMenu( hContextMenu, MF_STRING,					MENUID_EXIT,			_T( "Exit" )				);

						::SetForegroundWindow( this->m_hWnd );
						BOOL nSelectedIndex = ::TrackPopupMenu(	hContextMenu,
																TPM_LEFTALIGN | TPM_HORPOSANIMATION | TPM_RETURNCMD | TPM_LEFTBUTTON,
																pt.x,
																pt.y,
																0,
																this->m_hWnd,
																NULL );

						switch( nSelectedIndex )
						{
							case MENUID_EXIT		:
								::PostQuitMessage( 0 );
								break;
							case MENUID_UNTRAY_ALL	:
								{
								}
								break;
							case MENUID_ABOUT_DLG	:
								{
									::MessageBox( this->m_hWnd, _T( "TEST" ), _T( "TEST" ), MB_OK );
								}
								break;
							default					:
								if( nSelectedIndex >= MENUID_APPNAMES )
									CEventHookerWraper::GetInstance().UnTrayMe( szWindowCaptions[ nSelectedIndex - MENUID_APPNAMES ] );
								break;							
						}
			}
		}
	}
	catch( TCHAR * pszError )
	{
		::OutputDebugString( pszError );
	}
	catch( ... )
	{
	}

	return LRESULT();
}